package com.kingre.et.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;

import com.kingre.et.model.Questionnaire;
import com.kingre.et.model.User;
import com.kingre.et.service.IQuestionService;
import com.opensymphony.xwork2.ActionSupport;

public class AnswerQuestionAction extends ActionSupport {
    private static final long serialVersionUID = -4545800438878338015L;
    @Autowired
    private IQuestionService questionService;
    private List<Questionnaire> questions;
    private String[] questionValue;
    private String[] questionId;
    private Long acTime;
    private String strTime;
    private int page;

    public String start() {
        Integer userId = getCurrentUserId();
        questions = questionService.start(userId);
        page = questionService.getPage(userId);
        return SUCCESS;
    }

    public String next() {
        page++;
        if (questionId.length == 0 || questionId.length != questionValue.length)
            return start();
        questions = questionService.next(getCurrentUserId(), questionId, questionValue);
        if (questions == null || questions.size() == 0 || questions.isEmpty())
            return "display";
        else
            return SUCCESS;
    }

    public List<Questionnaire> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Questionnaire> questions) {
        this.questions = questions;
    }

    public void setQuestionValue(String[] questionValue) {
        this.questionValue = questionValue;
    }

    private Integer getCurrentUserId() {
        User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return currentUser.getId();
    }

    public Long getAcTime() {
        return acTime;
    }

    public void setAcTime(Long time) {
        acTime = time;
    }

    public String getStrTime() {
        return strTime;
    }

    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }

    public void setQuestionId(String[] questionId) {
        this.questionId = questionId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setqItem(List<String> qItem) {
        System.out.println(qItem);
    }

}
